<template>
	<view class="order-detail">
		<view class="top-box">
			<block v-if="orderInfo.status == 1">
				<view class="title">待发货</view>
			</block>
			<block v-if="orderInfo.status == 4">
				<view class="title">待收货</view>
			</block>
			<block v-else-if="orderInfo.status == 5">
				<view class="title">已完成</view>
				<view class="tips">感谢您对小店的光顾！</view>
			</block>
		</view>
		
		<view class="container">
			<view class="address-box" v-if="orderInfo.goods_type == 2">
				<view class="icon-box">
					<u-icon name="map-fill" color="#fff" size="40rpx"></u-icon>
				</view>
				<view class="content">
					<view class="text">
						<text class="uname">{{ orderInfo.addressee_name }}</text>
						<text class="phone">{{ orderInfo.addressee_telphone }}</text>
					</view>
					<view class="addressInfo">{{ orderInfo.address }}</view>
				</view>
			</view>
			<view class="goods-card">
				<view class="card-title">商品明细</view>
				<view class="goods-box">
					<view class="item" v-for="(item, index) in orderInfo.item" :key="index">
						<image class="image" :src="item.thumb" mode="aspectFill"></image>
						<view class="content">
							<view class="title u-line-1">{{ item.goods_name }}</view>
							<view class="specs-box">
								<text class="specs">规格：一份</text>
								<text class="number">x1</text>
							</view>
							<view class="price">
								<text class="icon">￥</text>
								<text>{{ item.amount }}</text>
							</view>
						</view>
					</view>
				</view>
				<view class="info">
					<!-- <view class="item">
						<view class="label">运费：</view>
						<view class="value">
							<text class="icon">￥</text>
							<text>0.00</text>
						</view>
					</view> -->
					<view class="item">
						<view class="label">{{ config.trade_currency_2 }}抵扣：</view>
						<view class="value">
							<text class="icon">￥</text>
							<text>{{ orderInfo.integral }}</text>
						</view>
					</view>
					<view class="item">
						<view class="label">实付款：</view>
						<view class="value">
							<text class="icon">￥</text>
							<text>{{ orderInfo.total_amount }}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="info-card">
				<view class="card-title">订单信息</view>
				<view class="item mt-20">
					<view class="value">订单编号：{{ orderInfo.order_sn }}</view>
					<view class="btn" @click="copyHandle(orderInfo.order_sn)">复制</view>
				</view>
				<view class="item mt-20">
					<view class="value">下单时间：{{ orderInfo.create_time }}</view>
				</view>
				<view class="item mt-20">
					<view class="value">支付时间：{{ orderInfo.pay_time }}</view>
				</view>
			</view>
			<block v-if="orderInfo.goods_type == 2">
				<view class="info-card" v-if="orderInfo.status == 4 || orderInfo.status == 5">
					<view class="card-title">快递信息</view>
					<view class="item mt-20">
						<view class="value">快递名称：{{ orderInfo.delivery }}</view>
					</view>
					<view class="item mt-20">
						<view class="value">快递单号：{{ orderInfo.transport_no }}</view>
						<view class="btn" @click="copyHandle(orderInfo.transport_no)">复制</view>
					</view>
				</view>
			</block>
		</view>

		<view class="bottom-box" v-if="orderInfo.status == 4">
			<button class="btn" @click="takeConfirm">确认收货</button>
		</view>
	</view>
</template>

<script>
	import { getOrderDetail, takeDelivery, getConfig } from '@/api/api'
	
	export default {
		data () {
			return {
				id: '',
				
				orderInfo: {},
				config: {},
			}
		},
		onLoad (options) {
			this.id = options.id
			this.loadData()
		},
		methods: {
			loadData () {
				getOrderDetail({ 'order_id': this.id }).then(res => {
					if (res) {
						this.orderInfo = res.data
					}
				})
				
				getConfig().then(res => {
					if (res) {
						this.config = res.data
					}
				})
			},
			copyHandle (data) {
				uni.setClipboardData({
					data,
					success: () => {
						console.log('success');
					}
				})
			},
			copyCode () {
				uni.setClipboardData({
					data: 'zxkRGvLk',
					success: () => {
						this.toMiniProgram()
					}
				})
			},
			toMiniProgram () {
				uni.navigateToMiniProgram({
					appId: '',
					success: (res) => {
						console.log(res)
					},
					fail: (err) => {
						uni.showToast({
							title: '跳转失败',
							icon: 'none'
						})
					}
				})
			},
			takeConfirm () {
				uni.showLoading({ mask: true })
				takeDelivery({ 'order_id': this.id }).then(res => {
					uni.hideLoading({ noConflict: true })
					if (res) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						
						setTimeout(() => {
							uni.$emit('onRefresh')
							uni.navigateBack()
						}, 500)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.order-detail {
	min-height: 100%;
	background-color: $bg-color;
	.top-box {
		padding: 40rpx 60rpx 60rpx;
		color: #fff;
		background-color: #000;
		.title {
			font-size: 40rpx;
			font-weight: bold;
		}
		.tips {
			margin-top: 20rpx;
			font-size: 28rpx;
		}
	}
	.container {
		padding: 24rpx;
		padding-bottom: 98rpx;
		padding-bottom: calc(98rpx + constant(safe-area-inset-bottom));
		padding-bottom: calc(98rpx + env(safe-area-inset-bottom));
		.address-box {
			margin-bottom: 24rpx;
			padding: 28rpx 30rpx;
			border-radius: 14rpx;
			background-color: #FFFFFF;
			display: flex;
			align-items: center;
			.icon-box {
				width: 80rpx;
				height: 80rpx;
				border-radius: 40rpx;
				background-color: $primary-color;
				display: flex;
				align-items: center;
				justify-content: center;
			}
			.content {
				flex: 1;
				padding: 0 20rpx 0;
				.addressInfo {
					font-size: 26rpx;
					color: #333;
				}
				.text {
					margin-bottom: 10rpx;
					font-size: 26rpx;
					.uname {
						color: #333333;
						margin-right: 10rpx;
					}
					.phone {
						font-size: 24rpx;
						color: #999;
					}
				}
				.item {
					font-size: 26rpx;
					&:not(:last-of-type) {
						margin-bottom: 10rpx;
					}
					.query {
						color: #999;
						font-size: 24rpx;
						border: 1rpx solid #999;
						border-radius: 24rpx;
						padding: 0 8rpx;
						margin-left: 12rpx;
					}
				}
			}
		}
		.goods-card {
			padding: 30rpx;
			border-radius: 14rpx;
			background-color: #fff;
			.card-title {
				color: #333;
				font-size: 28rpx;
				font-weight: bold;
			}
			.goods-box {
				margin-top: 22rpx;
				.item {
					display: flex;
					&:not(:last-child) {
						margin-bottom: 20rpx;
					}
				}
				.image {
					width: 140rpx;
					height: 140rpx;
					border-radius: 14rpx;
				}
				.content {
					margin-left: 20rpx;
					flex: 1;
					display: flex;
					flex-direction: column;
					.title {
						color: #333;
						font-size: 28rpx;
					}
					.specs-box {
						margin-top: auto;
						display: flex;
						align-items: center;
						justify-content: space-between;
						.specs {
							color: #888;
							font-size: 24rpx;
						}
						.number {
							color: #333;
							font-size: 24rpx;
						}
					}
					.price {
						margin-top: auto;
						color: #333;
						font-size: 28rpx;
						.icon {
							font-size: 16rpx;
						}
					}
				}
			}
			.info {
				margin-top: 22rpx;
				.item {
					display: flex;
					align-items: center;
					color: #666666;
					font-size: 28rpx;
					&:not(:last-child) {
						margin-bottom: 20rpx;
					}
					.label {
						flex: 1;
						text-align: right;
					}
					.value {
						width: 132rpx;
						.icon {
							font-size: 16rpx;
						}
					}
				}
			}
		}
		.info-card {
			margin-top: 24rpx;
			padding: 30rpx;
			border-radius: 14rpx;
			background-color: #fff;
			.card-title {
				color: #333;
				font-size: 28rpx;
				font-weight: bold;
			}
			.item {
				display: flex;
				align-items: center;
				.value {
					flex: 1;
					color: #666;
					font-size: 26rpx;
				}
				.btn {
					width: 100rpx;
					height: 40rpx;
					border: 1rpx solid #979797;
					color: #666;
					font-size: 26rpx;
					text-align: center;
					line-height: 40rpx;
				}
			}
		}
		.code-card {
			margin-top: 24rpx;
			padding: 44rpx;
			border-radius: 14rpx;
			background-color: #edffed;
			.title {
				color: #333333;
				font-size: 38rpx;
				font-weight: bold;
				text-align: center;
			}
			.code-box {
				margin-top: 66rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				.label {
					color: #333;
					font-size: 32rpx;
					font-weight: bold;
				}
				.code {
					margin-left: 26rpx;
					border-radius: 14rpx;
					border: 2rpx solid #c3f0c3;
					width: 388rpx;
					height: 100rpx;
					color: #333333;
					font-size: 36rpx;
					font-weight: bold;
					text-align: center;
					line-height: 100rpx;
				}
			}
			.btn {
				margin-top: 40rpx;
				border-radius: 100rpx;
				height: 100rpx;
				color: #fff;
				font-weight: bold;
				font-size: 36rpx;
				line-height: 100rpx;
				background-color: #333333;
			}
			.describe {
				margin-top: 40rpx;
				color: #333333;
				font-size: 26rpx;
				.describe-title {
					font-weight: bold;
				}
			}
		}
	}
	.bottom-box {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 10;
		height: 74rpx;
		padding: 0 50rpx;
		padding-bottom: 0;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		.btn {
			margin: 0;
			padding: 0;
			border-radius: 54rpx;
			width: 128rpx;
			height: 54rpx;
			color: #fff;
			font-size: 22rpx;
			line-height: 54rpx;
			background-color: #cd2a1b;
		}
	}
}
</style>
